package com.lvmama.rhino.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.lvmama.rhino.mapper.FlowStatisticsMapper;
import com.lvmama.rhino.pojo.ContinuityCode;
import com.lvmama.rhino.pojo.FlowData;
import com.lvmama.rhino.pojo.FlowStatisticsVo;
import com.lvmama.rhino.service.FlowStatisticsService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author yuanchi
 * @ClassName: FlowStatisticsServiceImpl
 * @Description: TODO
 * @date 2016年10月24日 上午11:19:43
 */

@Service("flowStatisticsService")
public class FlowStatisticsServiceImpl implements FlowStatisticsService {

    /**
     * 日志
     */
    protected Logger logger = Logger.getLogger(this.getClass());

    @Autowired
    private FlowStatisticsMapper flowStatisticsMapper;


    @Override
    public PageInfo<FlowStatisticsVo> landingPageTab(Map<String, Object> param) {
        PageHelper.startPage(Integer.valueOf(param.get("start_num").toString()), Integer.valueOf(param.get("page_size").toString()));
        List<FlowStatisticsVo> landingPages = flowStatisticsMapper.queryLandingTab(param);
        PageInfo<FlowStatisticsVo> page = new PageInfo<FlowStatisticsVo>(landingPages);
        return page;
    }

    @Override
    public Map<String, List<FlowStatisticsVo>> landingPageDepthTime(Map<String, Object> param) {
        long beginTime = new Date().getTime();
        loggerAssist("landingPageDepthTime", "start", beginTime, 0, "INFO");
        Map<String, List<FlowStatisticsVo>> resultMap = new HashMap<String, List<FlowStatisticsVo>>();
        List<FlowStatisticsVo> accessTimeMap = flowStatisticsMapper.queryAccessTimeRank(param);//访问时长
        List<FlowStatisticsVo> depthMap = flowStatisticsMapper.queryCrawlDepthRank(param);//访问深度
        List<FlowStatisticsVo> landingPageMap = flowStatisticsMapper.querylandingPageSum(param);//着落页数量和跳出率查询
        resultMap.put("AccessTime", accessTimeMap);
        resultMap.put("Depth", depthMap);
        resultMap.put("LandingPage", landingPageMap);
        long endTime = new Date().getTime();
        loggerAssist("landingPageDepthTime", "end", endTime, beginTime, "INFO");
        return resultMap;
    }

    @Override
    public List<FlowStatisticsVo> landingRegionData(Map<String, Object> param) {
        return flowStatisticsMapper.queryByRegion(param);//区域分布;
    }

    @Override
    public Map<String, List<FlowStatisticsVo>> landingPageRank(Map<String, String> param) {
        long beginTime = new Date().getTime();
        loggerAssist("landingPageRank", "start", beginTime, 0, "INFO");

        long endTime = new Date().getTime();

        loggerAssist("landingPageRank", "end", endTime, beginTime, "INFO");
        return null;
    }

    @Override
    public PageInfo<FlowStatisticsVo> queryLandingPageList(Map<String, Object> param) {
        PageHelper.startPage(Integer.valueOf(param.get("start_num").toString()), Integer.valueOf(param.get("page_size").toString()));
        List<FlowStatisticsVo> landingPages = flowStatisticsMapper.queryLandingPageList(param);
        PageInfo<FlowStatisticsVo> page = new PageInfo<FlowStatisticsVo>(landingPages);
        return page;
    }

    @Override
    public PageInfo<FlowStatisticsVo> queryAccessPageList(Map<String, Object> param) {
        PageHelper.startPage(Integer.valueOf(param.get("start_num").toString()), Integer.valueOf(param.get("page_size").toString()));
        List<FlowStatisticsVo> landingPages = flowStatisticsMapper.queryAccessPageList(param);
        PageInfo<FlowStatisticsVo> page = new PageInfo<FlowStatisticsVo>(landingPages);
        return page;
    }

    private void loggerAssist(String functionName, String message, long currentTime, long startTime, String loggerLv) {
        StringBuffer sb = new StringBuffer();
        sb.append(functionName);
        sb.append(" : ");
        sb.append(message);
        sb.append(" 当前时间:");
        sb.append(currentTime);
        if (currentTime > startTime) {
            sb.append(" 耗时:");
            sb.append(currentTime - startTime);
        }
        if ("ERROR".equals(loggerLv)) {
            logger.error(sb.toString());
        } else if ("DEBUG".equals(loggerLv)) {
            logger.debug(sb.toString());
        } else {
            logger.info(sb.toString());
        }


    }

    @Override
    public Long queryFlowStatisticsSum(Map<String, String> param) {
        Long amount = 0l;
        List<FlowStatisticsVo> flowList = flowStatisticsMapper.queryFlowStatisticsSum(param);
        if (flowList != null && !flowList.isEmpty()) {
            FlowStatisticsVo flowStatistics = flowList.get(0);
            if (flowStatistics != null) {
                amount = flowStatistics.getAmount();
            }

        }
        return amount;
    }

    @Override
    public PageInfo<FlowStatisticsVo> queryFlowFromList(Map<String, Object> param) {
        PageHelper.startPage(Integer.valueOf(param.get("pageCurrentNum").toString()), Integer.valueOf(param.get("pageSize").toString()));
        PageInfo<FlowStatisticsVo> pageInfo = new PageInfo<>(flowStatisticsMapper.queryFlowFromList(param));
        return pageInfo;
    }

    @Override
    public PageInfo<FlowStatisticsVo> queryFlowToList(Map<String, Object> param) {
        PageHelper.startPage(Integer.valueOf(param.get("pageCurrentNum").toString()), Integer.valueOf(param.get("pageSize").toString()));
        PageInfo<FlowStatisticsVo> pageInfo = new PageInfo<>(flowStatisticsMapper.queryFlowToList(param));
        return pageInfo;
    }

    @Override
    public Map<String, Long> queryFlowFromAndToSum(Map<String, Object> param) {
        Map<String, Long> result = new HashMap<String, Long>();
        Long amountF = 0l;
        Long amountT = 0l;
        List<FlowStatisticsVo> fromList = flowStatisticsMapper.queryFlowFromList(param);
        List<FlowStatisticsVo> toList = flowStatisticsMapper.queryFlowToList(param);
        for (FlowStatisticsVo flowStatistics : fromList) {
            amountF += flowStatistics.getAmount();
        }
        for (FlowStatisticsVo flowStatistics : toList) {
            amountT += flowStatistics.getAmount();
        }
        result.put("amountF",amountF);
        result.put("amountT",amountT);
        return  result;
    }

    @Override
    public List<FlowStatisticsVo> queryFlowbyChannel(Map<String, Object> param) {
        List<FlowStatisticsVo> flowList = flowStatisticsMapper.queryFlowbyChannel(param);
        return flowList;
    }

    @Override
    public List<FlowStatisticsVo> queryFlowbyChannelWap(Map<String, Object> param) {
        List<FlowStatisticsVo> flowList = flowStatisticsMapper.queryFlowbyChannelWap(param);
        return flowList;
    }

    @Override
    public List<FlowStatisticsVo> queryFlowbyApp(Map<String, Object> param) {
        List<FlowStatisticsVo> flowList = flowStatisticsMapper.queryFlowbyApp(param);
        return flowList;
    }

    @Override
    public List<ContinuityCode> queryContinuityCode() {
        return flowStatisticsMapper.queryContinuityCode();
    }

    @Override
    public FlowData queryFlowStatisticsData(Map<String, Object> param) {
        FlowData flow = flowStatisticsMapper.queryFlowStatisticsData(param);
        return flow;
    }


    @Override
    public List<FlowStatisticsVo> queryFlowDetailFromList(Map<String, Object> param) {
        return flowStatisticsMapper.queryFlowDetailFromList(param);
    }

    @Override
    public List<FlowStatisticsVo> queryFlowDetailToList(Map<String, Object> param) {
        return flowStatisticsMapper.queryFlowDetailToList(param);
    }
}
